package application;

import android.content.Context;
import android.content.SharedPreferences;

import com.lidroid.xutils.util.LogUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 管理本app的各项,设置项的 key 和 value
 *
 * Created by yanglei on 16/1/9.
 *
 * SharedPreferences.OnSharedPreferenceChangeListener listener1 = AppApplication.getAppKeyValueSet()
 * .addOnSetValueChangeListenerForKey(AppKeyValueSet.IsOnlyWiFiUploadAndDownload, new
 * SharedPreferences
 * .OnSharedPreferenceChangeListener() {
 *
 * @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
 * MessageUtil.showShortToast(context, "1");
 * }
 * });
 * SharedPreferences.OnSharedPreferenceChangeListener listener2 = AppApplication.getAppKeyValueSet()
 * .addOnSetValueChangeListenerForKey(AppKeyValueSet.IsOnlyWiFiUploadAndDownload, new
 * SharedPreferences
 * .OnSharedPreferenceChangeListener() {
 * @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
 * MessageUtil.showShortToast(context, "2");
 * AppApplication.getAppKeyValueSet().removeOnSetValueChangeListenerForKey(AppKeyValueSet
 * .IsOnlyWiFiUploadAndDownload, this);
 * }
 * });
 * AppApplication.getAppKeyValueSet().removeOnSetValueChangeListenerForKey(AppKeyValueSet.IsOnlyWiFiUploadAndDownload, listener1);
 */
public class UIKeyValueSet {

    private Context context = null;
    private SharedPreferences sharedPreferences = null;
    private Map<String, List<SharedPreferences.OnSharedPreferenceChangeListener>> mapOnChange = new HashMap();

    /*
    设置文件中的key
     */
    public final static String IsOnlyWiFiUploadAndDownload = "AppSet_IsOnlyWiFiUploadAndDownload";
    /**
     * 是否打开通知
     */
    public final static String IsNotificationOpen = "AppSet_IsNotificationOpen";

    public UIKeyValueSet(Context context) {
        this.context = context;
        sharedPreferences = context.getSharedPreferences(UIApplication.PackageName, Context.MODE_PRIVATE);

        init();
    }

    /**
     * 设置key的初始值 如:setInit(IsOnlyWiFiUploadAndDownload, false);
     */
    public void init(){}
    /**
     * 设置key的初始值 如:setInit(IsOnlyWiFiUploadAndDownload, false);
     */
    public void setInitKey(String key, Object defaultValue) {
        if (!sharedPreferences.contains(key)) {
            writeAppSetPreferences(key, defaultValue + "");
        }
    }

    /**
     * 加入app设置中有某一个key的值发生变化的时候的事件监听
     *
     * @param listener
     */
    public SharedPreferences.OnSharedPreferenceChangeListener addOnSetValueChangeListenerForKey(String key, SharedPreferences
            .OnSharedPreferenceChangeListener listener) {
        List<SharedPreferences.OnSharedPreferenceChangeListener> listOnChange = null;
        if (mapOnChange.containsKey(key)) {
            listOnChange = mapOnChange.get(key);
            listOnChange.add(listener);
        } else {
            listOnChange = new ArrayList();
            listOnChange.add(listener);
            mapOnChange.put(key, listOnChange);
        }
        return listener;
    }

    /**
     * 移除key监听事件
     *
     * @param key
     * @param listener
     */
    public void removeOnSetValueChangeListenerForKey(String key, SharedPreferences.OnSharedPreferenceChangeListener listener) {
        if (mapOnChange.containsKey(key)) {
            List<SharedPreferences.OnSharedPreferenceChangeListener> listOnChange = mapOnChange.get(key);
            if (listOnChange != null && listOnChange.contains(listener)) {
                listOnChange.remove(listener);
            }
        }
    }

    /**
     * 向本app设置文件中写入一个key:value
     *
     * @param key   参考:AppSet
     * @param value
     * @return
     */
    public boolean writeAppSetPreferences(String key, String value) {
        try {
            List<SharedPreferences.OnSharedPreferenceChangeListener> listOnChange = null;
            if (sharedPreferences.contains(key)) {
                String vlaue_temp = readAppSetPreferences(key);
                if (!vlaue_temp.equalsIgnoreCase(value)) {
                    if (mapOnChange.containsKey(key)) {
                        listOnChange = mapOnChange.get(key);
                    }
                }
            }
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString(key, value);
            editor.commit();
            if (listOnChange != null && listOnChange.size() > 0) {
                for (SharedPreferences.OnSharedPreferenceChangeListener item : listOnChange) {
                    item.onSharedPreferenceChanged(sharedPreferences, key);
                }

            }
            return true;
        } catch (Exception e) {
            LogUtils.e(e.getMessage() + "");
            return false;
        }
    }

    /**
     * 获得一个app设置文件中的值,根据key
     *
     * @param key 参考:AppSet
     * @return
     */
    public String readAppSetPreferences(String key) {
        try {

            String value = sharedPreferences.getString(key, null);
            return value;
        } catch (Exception e) {
            LogUtils.e(e.getMessage() + "");
            return null;
        }

    }

    /**
     * 返回所有设置及结果
     *
     * @return
     */
    public Map<String, ?> getAllKeyValueResult() {
        if (sharedPreferences != null) {
            return sharedPreferences.getAll();
        } else {
            return null;
        }
    }
}
